package com.summer.shopping.controller;

import com.summer.shopping.domain.table.ProductCategory;
import com.summer.shopping.service.ProductCategoryService;
import com.summer.shopping.utils.Result;
import com.summer.shopping.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 查询所有商品分类名称
     *
     * @return
     */
    @GetMapping("page")
    public Result<List<ProductCategory>> categoryShow(){
        log.info("查询所有商品分类");
        List<ProductCategory> category = productCategoryService.getAllCategory();
        return Result.ok(category);
    }


    /**
     * 新增分类
     * @return
     */
    @PostMapping
    public Result save(@RequestBody ProductCategory productCategory){

        log.info("新增分类：{}",productCategory);

        productCategoryService.saveCategory(productCategory);
        return Result.ok("成功");
    }


    /**
     * 批量删除
     * @param id
     * @return
     */

    @DeleteMapping
    public Result deleteById(@RequestParam Long id){
        log.info("删除分类:{}",id);
        productCategoryService.deleteByIds(id);
        return Result.ok("成功删除");
    }

    @PutMapping
    public Result update(ProductCategory productCategory){

        log.info("更新商品分类：{}",productCategory);

        productCategoryService.updateCategory(productCategory);
        return Result.ok("更新成功");
    }

    @GetMapping("/{id}")
    public Result<ProductCategory> getById(@PathVariable Long id){

        ProductCategory productCategory = productCategoryService.getById(id);
        return Result.ok(productCategory);
    }
}
